import { defineStore } from "pinia";
import { ref } from "vue";

export interface IUser {
  id: string;
  name: string;
}

export const useUserStore = defineStore("user", () => {
  const user = ref<IUser | null>(null);
  const token = ref<string | null>(null);
  const setUser = (userInfo: any) => {
    const user_info = userInfo;
    user.value = user_info;
    window.sessionStorage.setItem("user", JSON.stringify(user.value));
  };

  const getUser = () => {
    return user.value;
  };

  const setToken = (_token: string) => {
    token.value = _token;
    window.sessionStorage.setItem("tokens", JSON.stringify(_token));
  };

  const getToken = () => {
    return token.value;
  };

  const logout = () => {
    window.sessionStorage.removeItem("user");
    window.sessionStorage.removeItem("tokens");
    //todo  返回微信小程序
  };

  return {
    setUser,
    getUser,
    setToken,
    getToken,
    logout,
  };
});
